# _*_ coding:utf-8 _*_
# @Time  : 2023.03.27
# @Author: zizlee
import time

import requests
import pandas as pd
from urllib3 import disable_warnings
disable_warnings()


def read_index():
    df = pd.read_excel('数据指标-小麦.xlsx', engine='openpyxl')
    indexes = []
    for row in df.itertuples():
        indexes.append({
            'ID': row.指标ID.upper().strip(),
            'PLAT': row.PLAT.lower().strip()
        })
    return indexes


def is_exist(row):
    # 查询在本地是否存在
    url = 'https://210.13.218.130:9000/v1/datalib/table/exists/'
    r = requests.get(url, params={'plat': row['PLAT'], 'sid': row['ID']}, verify=False)
    if r.json()['data']:
        return True
    else:
        return False


def get_plat_info(row):
    pass


def add_index(row):
    url = 'https://210.13.218.130:9000/v1/datalib/table/'
    json_data = {
        'frequency': "月",
        'is_open': 1,
        'name_zh': "小麦:预测年度:产量:美国",
        'note': "",
        'raw_source': "美国农业部",
        'source_id': "S0113037",
        'source_plat': "wind",
        'startdate': "2000-01-31",
        'state': 1,
        'three_plat': "",
        'unit': "百万吨",
        'variety': "PM",
    }
    print(row)


def main():
    index_list = read_index()
    print(index_list)
    for i, row in enumerate(index_list):
        if is_exist(row):
            continue
        add_index(row)
        time.sleep(1)


if __name__ == '__main__':
    main()
